import { message } from 'antd';
import { deleteMaster, getMasterInfo, queryMasterList, updateMaster, updateMasterStatus, addMaster, changeMasterPwd } from '../services/api';

export default {
  namespace: 'masterManage',

  state: {
    masterInfo: {},
    masterUserList: [],
  },

  effects: {
    *fetch({ payload }, { call, put }) {
      const response = yield call(queryMasterList, payload);
      yield put({
        type: 'masterUserList',
        payload: response,
      });
    },
    *updateMaster({ payload, callback }, { call }) {
      const response = yield call(updateMaster, payload);
      if (response.code === 0) {
        message.success('更新成功');
        if (callback) callback();
      } else {
        message.error('更新失败');
      }
    },
    *deleteMaster({ payload, callback }, { call }) {
      const response = yield call(deleteMaster, payload);
      if (response.code === 0) {
        message.success('删除成功');
        if (callback) callback();
      } else {
        message.error('删除失败');
      }
    },
    *getMasterInfo({ payload,callback }, { call, put }) {
      const response = yield call(getMasterInfo, payload);
      if (response.code === 0) {
        yield put({
          type: 'masterInfo',
          payload: response,
        });
        if (callback) callback();
      }
      
    },
    *addMaster({ payload, callback }, { call }) {
      const response = yield call(addMaster, payload);
      if (response.code === 0) {
        message.success('新增成功');
      } else {
        message.error('新增失败');
      }
      if (callback) callback();
    },
    *updateMasterStatus({ payload, callback }, { call }) {
      const response = yield call(updateMasterStatus, payload);
      if (response.code === 0) {
        message.success('修改状态成功');
        if (callback) callback();
      } else {
        message.error('修改状态失败');
      }
    },
    *changeMasterPwd({ payload, callback }, { call }) {
      const response = yield call(changeMasterPwd, payload);
      if (response.code === 0) {
        message.success('修改密码成功');
        if (callback) callback();
      } else {
        message.error('修改密码失败');
      }
    },
  },
  reducers: {
    masterUserList(state, action) {
      return {
        ...state,
        masterUserList: action.payload.data,
      };
    },
    masterInfo(state, action) {
      return {
        ...state,
        masterInfo: action.payload.data || {},
      };
    },
  },
};
